package net.mimai.vultrue.bus.enums;

import net.mimai.vultrue.framework.utils.FrameworkUtils;
import net.mimai.vultrue.framework.utils.JudgeUtils;

/**
 * @ClassName AuditStatus
 * @Description 审批状态
 * @Author 方丈
 * @Date 2021/2/2 9:36
 * @Version 1.0.0
 **/
public enum AuditStatus {
    WAIT(1,"提交等待"),
    S1PASS(2,"审核通过"),
    S1BACK(3,"审核驳回"),
    S2PASS(4,"复核通过"),
    S2BACK(5,"复核驳回"),

    CANCEL(6,"取消"),
    ;
    Integer key;
    String desc;

    AuditStatus(int key, String desc) {
        this.key = key;
        this.desc = desc;
    }

    public Integer getKey() {
        return key;
    }

    public void setKey(Integer key) {
        this.key = key;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static Boolean isMember(int key){
        AuditStatus[] vs= AuditStatus.values();
        for(AuditStatus h:vs){
            if(JudgeUtils.equals(key,h.getKey())){
                return true;
            }
        }
        return false;
    }

    public static AuditStatus getByKey(int key){
        AuditStatus[] vs= AuditStatus.values();
        for(AuditStatus h:vs){
            if(JudgeUtils.equals(key,h.getKey())){
                return h;
            }
        }
        return null;
    }


    /**
     * 校验状态流转合法性
     */
    public static void checkChange(int originStatusKey, int statusKey){
        AuditStatus originStatus= getByKey(originStatusKey);
        if(JudgeUtils.isNull(originStatus)){
            FrameworkUtils.throwFailRet();
        }
        AuditStatus status= getByKey(statusKey);
        if(JudgeUtils.isNull(status)){
            FrameworkUtils.throwFailRet();
        }
        switch (originStatus){
            case WAIT:
                if(JudgeUtils.notEquals(status,S1PASS)&&JudgeUtils.notEquals(status,S1BACK)){
                    FrameworkUtils.throwFailRet();
                }
                break;
            case S1PASS:
                if(JudgeUtils.notEquals(status,S2PASS)&&JudgeUtils.notEquals(status,S2BACK)){
                    FrameworkUtils.throwFailRet();
                }
                break;
            case S1BACK:
            case S2BACK:
                if(JudgeUtils.notEquals(status,WAIT)){
                    FrameworkUtils.throwFailRet();
                }
                break;
            default:
                FrameworkUtils.throwFailRet();
        }
    }
}
